#    Copyright 2012 New Mexico EPSCoR
#
#    This file is part of Teacher's P.E.T.
#
#    Teacher's P.E.T. is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Teacher's P.E.T. is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Teacher's P.E.T..  If not, see <http://www.gnu.org/licenses/>.

from django.conf.urls.defaults import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
import os

import djangooptions
djangooptions.autodiscover()
#urlpatterns = djangooptions.urlpatterns()

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

from apps import management
management.autodiscover()

#Error pages
def server_error(request, template_name='500.html'):
    """
    500 error handler.

    Templates: `500.html`
    Context: None
    """
    return render_to_response(template_name,
        context_instance = RequestContext(request)
    )
handler500 = 'server_error'

#urlpatterns += patterns('',
#    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root' : os.path.join(settings.SITE_ROOT, 'static')}),
#    (r'^(.*?)/static/(?P<path>.*)$', 'django.views.static.serve', {'document_root' : os.path.join(settings.SITE_ROOT, 'static')}),
#)
#        
#urlpatterns += patterns('',
#    (r'^accounts/login/', 'views.login', {}, 'login'),
#    (r'^accounts/logout/', 'views.logout', {}, 'logout'),
#    (r'^accounts/password_reset/done/', 'views.password_reset_done'),
#    (r'^accounts/password_reset/complete/', 'views.password_reset_complete'),
#    (r'^accounts/password_reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/', 'views.password_reset_confirm'),
#    (r'^accounts/password_reset/', 'views.password_reset', {}, 'password_reset'), 
#    
#    (r'(?P<course>.+?)/accounts/login/', 'views.login', {}, 'login'),
#    (r'(?P<course>.+?)/accounts/logout/', 'views.logout', {}, 'logout'),
#    (r'(?P<course>.+?)/accounts/password_reset/done/', 'views.password_reset_done'),
#    (r'(?P<course>.+?)/accounts/password_reset/complete/', 'views.password_reset_complete'),
#    (r'(?P<course>.+?)/accounts/password_reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/', 'views.password_reset_confirm'),
#    (r'(?P<course>.+?)/accounts/password_reset/', 'views.password_reset', {}, 'password_reset'), 
#    )
#
#urlpatterns += patterns('',
#    (r'^admin/', include(admin.site.urls)),
#    (r'^assignments/', include('apps.assignments.urls')),
#    (r'^contact/', include('apps.contact.urls')),
#    (r'^files/', include('apps.files.urls')),  
#    (r'^grades/', include('apps.gradebook.urls')),
#    (r'^help/', include('apps.help.urls')),
#    (r'^manage/', include('apps.management.urls')),
#    (r'^news/', include('apps.news.urls')),    
#    (r'^polls/', include('apps.polls.urls')),
#    (r'^users/', include('apps.participants.urls')),
#    
#    (r'(.+?)/admin/', include(admin.site.urls)),
#    (r'(?P<course>.+?)/assignments/', include('apps.assignments.urls')),
#    (r'(?P<course>.+?)/contact/', include('apps.contact.urls')),
#    (r'(?P<course>.+?)/files/', include('apps.files.urls')),
#    (r'(?P<course>.+?)/help/', include('apps.help.urls')),
#    (r'(?P<course>.+?)/grades/', include('apps.gradebook.urls')),
#    (r'(?P<course>.+?)/manage/', include('apps.management.urls')),
#    (r'(?P<course>.+?)/news/', include('apps.news.urls')),    
#    (r'(?P<course>.+?)/polls/', include('apps.polls.urls')),
#    (r'(?P<course>.+?)/users/', include('apps.participants.urls')),
#
#    (r'',include('apps.pages.urls'))
#    )

solo_patterns = (
    (r'^accounts/login/', 'views.login', {}, 'login'),
    (r'^accounts/logout/', 'views.logout', {}, 'logout'),
    (r'^accounts/password_reset/done/', 'views.password_reset_done'),
    (r'^accounts/password_reset/complete/', 'views.password_reset_complete'),
    (r'^accounts/password_reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/', 'views.password_reset_confirm'),
    (r'^accounts/password_reset/', 'views.password_reset', {}, 'password_reset'),
    
    (r'^feeds/', include('apps.feeds.urls')),
    (r'^manage/', include('apps.management.urls')),
    (r'^contact/', include('apps.contact.urls')),
    (r'^files/', include('apps.files.urls')),  
    (r'^grades/', include('apps.gradebook.urls')),
    (r'^help/', include('apps.help.urls')),
    (r'^forum/', include('apps.forum.urls')),
    (r'^news/', include('apps.news.urls')),    
    (r'^users/', include('apps.participants.urls')),
    (r'^activities/', include('apps.activities.urls')),
    )

course_patterns = []
import string
solo_patterns = list(solo_patterns);
for item in solo_patterns:
    new_item = [string.replace(item[0],r'^',r'(?P<course>.+?)/')]; new_item.extend(item[1:])
    course_patterns.append(tuple(new_item))
    
solo_patterns.append((r'^admin/', include(admin.site.urls)))
solo_patterns.append((r'^tinymce/', include('tinymce.urls')))
course_patterns.append((r'(.+?)/admin/', include(admin.site.urls)))
course_patterns.append((r'(?P<course>.+?)/',include('apps.course.urls')))

urlpatterns = djangooptions.urlpatterns()
urlpatterns += patterns('',*tuple(solo_patterns))
urlpatterns += patterns('',*tuple(course_patterns))
urlpatterns += patterns('',*tuple(solo_patterns))
urlpatterns += patterns('',(r'^$',include('apps.course.urls')))
